package com.songlanyun.modules.shop.controller.api;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.modules.exception.ShopException;
import com.songlanyun.common.utils.Constant;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.shop.entity.ShopApplyEntity;
import com.songlanyun.modules.shop.model.dto.ShopApplyDTO;
import com.songlanyun.modules.shop.service.ShopApplyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;


/**
 * 会员申请商家表 - api
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-04 16:06:34
 */
@RestController
@RequestMapping("api/yxt/shopapply")
public class ShopApplyApiController extends AbstractApiController {
    @Autowired
    private ShopApplyService shopApplyService;

    /**
     * 会员查询自己的会员申请记录
     */
    @GetMapping("/shopApplyInfo")
//    @RequiresPermissions("account:shopApply:list")
    public R shopApplyInfo(@RequestParam(value = "union_business", required = false) Boolean unionBusiness) {
        ShopApplyEntity shopApplyEntity = shopApplyService.getOneShopApply(getUserId(true), ObjectUtil.isNull(unionBusiness) ? false : unionBusiness);
        if (shopApplyEntity != null) {
            shopApplyEntity.loadShop();
        }
        return R.ok().put("data", shopApplyEntity);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("account:shopApply:info")
    public R info(@PathVariable("id") Long id) {
        ShopApplyEntity shopApply = shopApplyService.getById(id);

        return R.ok().put("data", shopApply);
    }


    /**
     * 会员申请商家
     */
    @RepeatSubmit("3")
    @PostMapping("/apply")
    public R apply(@RequestBody ShopApplyDTO dto) {
        ValidatorUtils.validateEntity(dto);

        Long userId = getUserId(true);
        //申请列表
        ShopApplyEntity shopApplyEntity = shopApplyService.getOneShopApply(userId, ObjectUtil.isNull(dto.getUnionBusiness()) ? false : dto.getUnionBusiness());

        if (ObjectUtil.isNotNull(shopApplyEntity)) {
            //已通过
            if (Constant.AUDITPASS == shopApplyEntity.getAuditStatus()) {
                throw new RRException(ShopException.ACCOUNT_AUDIT_PASS);
            }
            //等待审核
            if (Constant.AUDITWAIT == shopApplyEntity.getAuditStatus()) {
                throw new RRException(ShopException.ACCOUNT_AUDIT_WAIT);
            }
        } else {
            //如果没有记录，则新建一条记录
            shopApplyEntity = new ShopApplyEntity();
        }

        BeanUtil.copyPropertiesIgnoreNull(dto, shopApplyEntity);
        shopApplyEntity.setUserId(userId);

        //如果有记录，并且审核失败的话，修改之前的申请记录
        shopApplyEntity.setAuditStatus(Constant.AUDITWAIT);//审核状态
        shopApplyEntity.setApplyTime(new Date());

        shopApplyService.saveOrUpdate(shopApplyEntity);

        return R.ok();
    }


}
